//
//  LoginViewController.swift
//  newSwift
//
//  Created by MAC on 2023/10/18.
//

import UIKit


enum LoginMode {
    case verification
    case password
}

class LoginViewController : BaseViewController {

    lazy var titleLabel : UILabel = {
        let label = UILabel()
        
        label.font = .systemFont(ofSize: 28, weight: .medium)
        label.textColor = UIColor.init(hex: 0x333333)!
        
        label.text = "手机号登录/注册"
        
        return label
    }()
    lazy var tipsLabel : UILabel = {
        let label = UILabel()
        
        label.font = .systemFont(ofSize: 14)
        label.textColor = UIColor.init(hex: 0xABABAB)!
        
        label.text = "未注册的手机验证后自动创建账户"
        
        return label
    }()
    
    lazy var phoneTextField : UITextField = {
        let textField = UITextField()
        
        textField.font = .systemFont(ofSize: 18)
        textField.textColor = UIColor.init(hex: 0x333333)!
        
        textField.placeholder = "输入手机号"
        
        return textField
    }()
    
    lazy var verificationBtn : UIButton = {
        let button = UIButton()
        
        button.setTitle("获取验证码", for: .normal)
        button.setBackgroundColor(color: UIColor.init(hex: 0xE4E4E4)!, forState: .normal)
        button.titleLabel?.font = .systemFont(ofSize: 18, weight: .light)
        button.setTitleColor(UIColor.init(hex: 0x333333)!, for: .normal)
        button.layerCornerRadius = 25
        
        button.addTarget(self, action: #selector(clickVerificationButton), for: .touchUpInside)
        return button
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.init(hexString: "#F6E1D0")
        setupViews()
    }
    
    //点击获取验证码
    @objc func clickVerificationButton(){
        
        UserDefaults().setValue(true, forKey: "isLogin")
        
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "SwicthRootViewControllerNotification"), object: nil)
    }
    
    deinit {
        print("登录控制器被销毁了 -------")
    }
}



/// 布局
extension LoginViewController {
    
    
    
    
    func setupViews(){
        
        view.addSubview(titleLabel)
        view.addSubview(tipsLabel)
        view.addSubview(verificationBtn)
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(85 + statusHeight)
            make.left.equalToSuperview().offset(28)
        }
        
        tipsLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(5)
            make.left.equalTo(titleLabel.snp.left)
        }
        
        //手机号的背景
        let phoneBgView : UIView = UIView()
        view.addSubview(phoneBgView)
        phoneBgView.backgroundColor = .white
        phoneBgView.layerCornerRadius = 25
        
        phoneBgView.snp.makeConstraints { make in
            make.left.equalTo(titleLabel.snp.left)
            make.top.equalTo(titleLabel.snp.bottom).offset(50)
            make.right.equalTo(-28)
            make.height.equalTo(50)
        }
        
        //+86
        let countryCodeLabel = UILabel()
        phoneBgView.addSubview(countryCodeLabel)
        countryCodeLabel.font = .systemFont(ofSize: 18, weight: .medium)
        countryCodeLabel.textColor = UIColor.init(hex: 0x333333)!
        countryCodeLabel.text = "+86"
        
        countryCodeLabel.snp.makeConstraints { make in
            make.left.equalTo(25)
            make.width.equalTo(38)
            make.centerY.equalToSuperview()
        }
        
        phoneBgView.addSubview(phoneTextField)
        phoneTextField.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.height.equalToSuperview()
            make.left.equalTo(countryCodeLabel.snp.right).offset(5)
            make.right.equalTo(-25)
        }
    
        
        verificationBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(phoneBgView.snp.bottom).offset(40)
            make.height.equalTo(50)
            make.width.equalTo(screenWidth-(28*2))
        }
        
    }
    
    
}
